package com.seleniti.enpareja.android.control.navigation;

import org.ioc.commons.flowcontrol.placecontroller.IsPlace;
import org.ioc.commons.impl.android.flowcontrol.activitymapper.Place;
import org.ioc.commons.impl.android.flowcontrol.placecontroller.AndroidPlaceController.PlaceMapper;
import org.ioc.commons.utils.Format;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.common.AccountPicker;
import com.seleniti.enpareja.android.WebViewActivity;
import com.seleniti.enpareja.android.ui.view.AppSettingsActivity;
import com.seleniti.enpareja.android.ui.view.CoupleRequestsViewWidget;
import com.seleniti.enpareja.android.ui.view.GetCoupleViewWidget;
import com.seleniti.enpareja.android.ui.view.HomeTasksViewWidget;
import com.seleniti.enpareja.android.ui.view.LoginViewWidget;
import com.seleniti.enpareja.android.ui.view.MainViewWidget;
import com.seleniti.enpareja.android.ui.view.MealsViewWidget;
import com.seleniti.enpareja.android.ui.view.ShoppingListViewWidget;
import com.seleniti.enpareja.android.ui.view.SignUpViewWidget;
import com.seleniti.enpareja.android.ui.view.SolveNoPasswordSetViewWidget;
import com.seleniti.enpareja.android.ui.view.UserSettingsViewWidget;
import com.seleniti.enpareja.controller.control.place.EnParejaPlace;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.BoardType;

public class AppPlaceMapper implements PlaceMapper {

	public static final int REQUEST_CODE_EMAIL = 1;
	public static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICE = 2;
	public static final int REQUEST_CODE_GAE_USER_LOGGED_IN = 3;
	public static final int CHECK_FOR_RESET_REQUIRED = 4;

	public AppPlaceMapper() {
	}

	@SuppressWarnings({ "unchecked", "incomplete-switch" })
	@Override
	public <P extends Enum<? extends IsPlace>> Place<P> getPlace(P place, Object... params) {
		EnParejaPlace cmPlace = (EnParejaPlace) place;
		// EnParejaView<?, ?, ?, ?> view = null;
		Place<P> androidPlace = null;
		Bundle extras = new Bundle();

		switch (cmPlace) {
		case Main:
			androidPlace = new Place<P>((P) cmPlace, new MainViewWidget());
			break;
		case LoginPlace:
			androidPlace = new Place<P>((P) cmPlace, new LoginViewWidget());
			break;
		case SignUp:
			androidPlace = new Place<P>((P) cmPlace, new SignUpViewWidget());
			break;
		case CoupleRequests:
			androidPlace = new Place<P>((P) cmPlace, new CoupleRequestsViewWidget());
			break;
		case GetCouple:
			androidPlace = new Place<P>((P) cmPlace, new GetCoupleViewWidget());
			break;
		case ShoppingList:
			androidPlace = new Place<P>((P) cmPlace, new ShoppingListViewWidget());
			break;
		case GoogleLoginPlace:
			Intent glpIntent = AccountPicker.newChooseAccountIntent(null, null,
					new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);

			androidPlace = new Place<P>((P) cmPlace, glpIntent, REQUEST_CODE_EMAIL);

			break;
		case UserSettings:
			boolean passwordChangeEnabled;
			try {
				passwordChangeEnabled = (params.length >= 3 && params[2] != null) ? Boolean.valueOf(String
						.valueOf(params[2])) : null;
			} catch (Exception e) {
				passwordChangeEnabled = true;
			}

			androidPlace = new Place<P>((P) cmPlace, new UserSettingsViewWidget(passwordChangeEnabled));
			break;
		case AppSettings:
			androidPlace = new Place<P>((P) cmPlace, AppSettingsActivity.class, CHECK_FOR_RESET_REQUIRED);
			break;
		case SolveNoPasswordSet:
			androidPlace = new Place<P>((P) cmPlace, new SolveNoPasswordSetViewWidget());
			break;
		case HomeTasks:
			androidPlace = new Place<P>((P) cmPlace, new HomeTasksViewWidget());
			break;
		case Meals:
			androidPlace = new Place<P>((P) cmPlace, new MealsViewWidget());
			break;
		case SharedExpenses:
		case WorkShifts:
		case PendingShopping:
			androidPlace = new Place<P>((P) cmPlace, WebViewActivity.class);

			extras.putString(WebViewActivity.URL_QUERY_KEY, Format.substitute(cmPlace.name()));
			break;
			
		}

		if (androidPlace != null && !extras.isEmpty()) {
			androidPlace.setExtras(extras);
		}

		return androidPlace;
	}

	@SuppressWarnings("incomplete-switch")
	public <P extends Enum<? extends IsPlace>> BoardType getBoardType(P place) {
		EnParejaPlace cmPlace = (EnParejaPlace) place;
		BoardType boardType = SliderPanel.BoardType.WITHOUT_LOGO;

		switch (cmPlace) {
		case Main:
		case LoginPlace:
		case SignUp:
		case ExternalLoginPlace:
		case GoogleLoginPlace:
			boardType = SliderPanel.BoardType.WITH_LOGO;
			break;
		case AppSettings:
			/*
			 * Keep current
			 */
			boardType = null;
			break;
		}
		return boardType;
	}

}
